# coding: utf-8

from md5 import md5

def main():
	inf = file('name.dat', 'r')
	ouf = file('nameout.dat', 'w')
	namelist = [line.strip().split('\t') for line in inf]
	namelist.sort(key = lambda l: (l[1], l[0]))
	for t in namelist:
		name_hash = 0
		for i in t[0]:
			name_hash = (ord(i) + (name_hash << 6) + (name_hash << 16) - name_hash)
		t.append(name_hash ^ ord(t[0][0]))
	print namelist
	for t in namelist:
		ouf.write(str(t[2]%120)+'\t'+t[0]+'\t'+t[1]+'\n')
	inf.close()
	ouf.close()

if __name__ == '__main__':
	main()
